using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ShopForWeb.Model.Basic;
using ShopForWeb.Model.DBContexts;

namespace ShopForWeb.Services
{
    public class ShopCarService
    {
        private ShopDBContext _db;
        public ShopCarService(ShopDBContext db)
        {
            _db=db;
        }
    public async Task<List<ShopCar>> GetAllAsync(long userId)
   {
     return await _db.ShopCarSet
           .Where(shopcar => shopcar.UserId == userId)
            .ToListAsync();
   }
      public async Task<int> AddNewAsync(ShopCar car)
     {

         _db.Entry<ShopCar>(car).State = EntityState.Added;
         return await _db.SaveChangesAsync();
       }
        public async Task<int> RemoveAsync(long productId, long userId)
       {
         var entity = _db.Entry<ShopCar>(new ShopCar()
          {
             ProductId = productId,
            UserId = userId
          });
         entity.State = EntityState.Deleted;
         return await _db.SaveChangesAsync();
   }

       public async Task<int> ClearAsync(long userId)
  {


         var shopCars = await _db.ShopCarSet.Where(sc => sc.UserId == userId).ToListAsync();

         _db.ShopCarSet.RemoveRange(shopCars);
         return await _db.SaveChangesAsync();
   }
    }
}